var a = require("../../utils/api.js"), t = getApp();

Page({
    data: {
        optionList: [ {
            status: 0,
            name: "本期商品",
            brief: "进行中"
        }, {
            status: 1,
            name: "下期商品",
            brief: "限时开始"
        } ],
        currentTab: 0,
        page: [ 1, 2, 3, 4, 5 ],
        pageFooter: 1,
        checkAll: !1,
        del_count: 0,
        currentPage: 1
    },
    onLoad: function(e) {
        var o = this;
        a.GetTeamGoods({
            openid: t.globalData.openid,
            type: o.data.currentTab,
            page: o.data.currentPage
        }).then(function(a) {
            o.setData({
                info: a.data.data,
                total: a.data.total,
                page: a.data.page,
                all_id: a.data.all_id,
                del_count: a.data.del_count,
                page_end: a.data.page[a.data.page.length - 1],
                currentTab: o.data.currentTab
            }), a.data.data[0].at_end_time > 0 && (o.setData({
                atTime: a.data.data[0].at_end_time
            }), o.countDown());
        });
    },
    switchNav: function(e) {
        var o = this, n = e.currentTarget.dataset.current;
        a.GetTeamGoods({
            openid: t.globalData.openid,
            type: n,
            page: 1
        }).then(function(a) {
            o.setData({
                info: a.data.data,
                total: a.data.total,
                page: a.data.page,
                all_id: a.data.all_id,
                del_count: a.data.del_count,
                page_end: a.data.page[a.data.page.length - 1],
                currentPage: 1,
                currentTab: n
            }), a.data.data[0].at_end_time > 0 && 0 == n && (o.setData({
                atTime: a.data.data[0].at_end_time
            }), o.countDown()), a.data.data[0].at_start_time > 0 && 1 == n && (o.setData({
                atTime: a.data.data[0].at_start_time
            }), o.countDown());
        });
    },
    selectalltap: function(a) {
        var t = this, e = a.detail.value, o = !1, n = "";
        e == e[0] && (o = !0);
        for (var d = 0; d < t.data.info.length; ++d) e && "" != e ? (t.data.info[d].checked = !0, 
        n += "," + t.data.info[d].gcg_id) : t.data.info[d].checked = !1;
        t.setData({
            checkAll: o,
            info: t.data.info,
            sub_ids: n
        });
    },
    checkboxChange: function(a) {
        for (var t = this, e = a.detail.value, o = "", n = 0; n < t.data.info.length; ++n) {
            t.data.info[n].checked = !1;
            for (var d = 0; d < e.length; d++) if (e[d] == t.data.info[n].gcg_id) {
                t.data.info[n].checked = !0, o += "," + t.data.info[n].gcg_id;
                break;
            }
        }
        "" == o && t.setData({
            checkAll: !1
        }), t.setData({
            info: t.data.info,
            sub_ids: o
        });
    },
    upGoods: function(e) {
        var o = this;
        console.log(e.currentTarget.dataset.type);
        var n = o.data.sub_ids;
        if ("" != e.currentTarget.dataset.type && void 0 != e.currentTarget.dataset.type && "all" == e.currentTarget.dataset.type && (n = o.data.all_id, 
        console.log(n)), "" == n || void 0 == n) return wx.showToast({
            title: "未选择任何商品",
            icon: "none",
            duration: 2e3
        }), !1;
        a.TeamOpenGoods({
            openid: t.globalData.openid,
            ids: n
        }).then(function(a) {
            0 == a.errno ? (wx.showToast({
                title: a.message,
                icon: "success"
            }), o.setData({
                sub_ids: "",
                checkAll: !1,
                del_count: 0
            }), o.onLoad()) : wx.showToast({
                title: a.message,
                icon: "none"
            });
        });
    },
    downGoods: function(e) {
        var o = this;
        console.log(e);
        var n = o.data.sub_ids;
        if (console.log(n), "" == n || void 0 == n) return wx.showToast({
            title: "未选择任何商品",
            icon: "none",
            duration: 2e3
        }), !1;
        a.TeamCLoseGoods({
            openid: t.globalData.openid,
            ids: n
        }).then(function(a) {
            0 == a.errno ? (wx.showToast({
                title: a.message,
                icon: "success"
            }), o.setData({
                sub_ids: "",
                checkAll: !1,
                del_count: 0
            }), o.onLoad()) : wx.showToast({
                title: a.message,
                icon: "none"
            });
        });
    },
    gotoPage: function(e) {
        var o = this, n = e.currentTarget.dataset.page;
        a.GetTeamGoods({
            openid: t.globalData.openid,
            page: n,
            type: o.data.currentTab
        }).then(function(a) {
            console.log(a.data.page), console.log(a.data.page.length), o.setData({
                info: a.data.data,
                total: a.data.total,
                page: a.data.page,
                all_id: a.data.all_id,
                del_count: a.data.del_count,
                page_end: a.data.page[a.data.page.length - 1],
                currentPage: n
            }), a.data.data[0].at_end_time > 0 && (o.setData({
                atTime: a.data.data[0].at_end_time
            }), o.countDown());
        });
    },
    timeFormat: function(a) {
        return a < 10 ? "0" + a : a;
    },
    countDown: function() {
        var a = new Date().getTime(), t = [], e = 1e3 * parseInt(this.data.atTime), o = null;
        if (e - a > 0) {
            var n = (e - a) / 1e3, d = parseInt(n / 86400), i = parseInt(n % 86400 / 3600), s = parseInt(n % 86400 % 3600 / 60), c = parseInt(n % 86400 % 3600 % 60);
            o = {
                day: this.timeFormat(d),
                hou: this.timeFormat(i),
                min: this.timeFormat(s),
                sec: this.timeFormat(c)
            };
        } else o = {
            day: "00",
            hou: "00",
            min: "00",
            sec: "00"
        };
        t.push(o), this.setData({
            countDownList: t
        }), setTimeout(this.countDown, 1e3);
    }
});